package com.cqyc.shop.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.comm.commException;
import com.cqyc.shop.services.CartServices;

@WebServlet(urlPatterns="/cartdelete.do")
public class ShowCartDeleteServlet extends HttpServlet{
	private static final long serialVersionUID = -1000905091177808997L;
	private Logger logger = LoggerFactory.getLogger(getClass());
	private CartServices cartServices = new CartServices();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("购物车删除表演开始");
		try {
			//1:从前台获取参数
			String id = request.getParameter("id");
			//2:数据转换
			Integer idd = Integer.valueOf(id);
			logger.debug("{}",idd);
			
			//3:数据校验
			if(idd==null) {
				throw new commException("您要删除的对象怕是心里没点逼数哟");
			}
			//4:业务处理
			cartServices.delete(idd);
			//5:跳转
			response.sendRedirect(request.getContextPath()+"/joincart.do");
		} catch (Exception e) {
			logger.error("{}",e.getMessage());
			request.getRequestDispatcher("/showCart.jsp").forward(request, response);
		}
	}
	
}
